package cn.lut.crowd.handler;

import cn.lut.constant.CrowdConstant;
import cn.lut.crowd.entity.po.MemberPO;
import cn.lut.crowd.service.api.MemberService;
import cn.lut.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @user:dell
 * @date:2021年01月07 17:06
 */
@RestController
public class MemberProviderHandler {
    @Autowired
    private MemberService memberService;
    @RequestMapping("/save/member/remote")
    public ResultEntity<String> saveMember(@RequestBody MemberPO memberPO){
        try {
            memberService.saveMember(memberPO);
            return ResultEntity.successWithOutData();
        } catch (Exception e) {
          if(e instanceof DuplicateKeyException){
              return ResultEntity.failed(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
          }
          return ResultEntity.failed(e.getMessage());
        }
    }
    @RequestMapping("/get/memberpo/by/login/acct/remote")
  public   ResultEntity<MemberPO> getMemberPOByLoginAcctRemote(@RequestParam("loginacct") String loginacct){
//     调用本地service完成查询
        try {
            MemberPO memberPO=  memberService.getMemberPOByLoginAcct(loginacct);
//            2.如果没有抛出异常，name就返回成功的结果
            return ResultEntity.successWithData(memberPO);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        3.如果捕获到异常则返回失败的结果
        return null;
    }
}
